package com.xy.etl.ibatis.node.impl;


import com.xy.etl.ibatis.core.XySQLExePara;
import com.xy.etl.ibatis.core.XySQLTaskContext;
import com.xy.etl.ibatis.itf.XySQLRegister;
import com.xy.etl.ibatis.kit.XySQLScriptKit;
import com.xy.etl.ibatis.node.XySQLSuperNode;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
@Slf4j
public class XySQLWhereNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "where";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLWhereNode();
    }

    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        List<XySQLExePara> resList = super.boundSql(task);
        if (resList.size() > 0) {
            XySQLExePara para;
            for (int i = 0; i < resList.size(); i++) {
                para = resList.get(i);
                String sql = para.getSqlFragment();
                if (sql == null || sql.trim().length() <= 4) {
                    log.debug("WHERE 内部SQL片段过小(" + sql + ")");
                    continue;
                }
                sql = XySQLScriptKit.trim(sql, "AND ", " AND");
                resList.set(i, new XySQLExePara(" WHERE " + sql, para.getValue(), para.isContainNull()));
                break;
            }

        }
        return resList;
    }
}
